<template>
  <div class="managerWrapper">
    <div class="handle-box">
      <el-input v-model="paramObj.name" placeholder="姓名" class="handle-input mr10"></el-input>
      <el-button type="primary" icon="el-icon-search" @click="search">查询</el-button>
      <el-button type="primary" icon="edit" @click="create()" class="create-btn">新建团长</el-button>
    </div>

    <el-table :data="tableData" stripe style="width: 100%">
      <el-table-column prop="name" label="姓名" align="center"></el-table-column>
      <el-table-column prop="phone" label="手机号" align="center"></el-table-column>
      <el-table-column prop="address" label="地址" align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="rewardMoney" label="已获佣金" align="center"></el-table-column>
      <el-table-column prop="frozenMoney" label="冻结佣金" align="center"></el-table-column>
      <el-table-column prop="nobalanceMoney" label="未结算佣金" align="center"></el-table-column>
      <el-table-column prop="status" label="状态" align="center" :formatter="statusFormat"></el-table-column>
      <el-table-column label="操作" align="center" width="450">
        <template slot-scope="scope">
          <el-button size="small" @click="handleView(scope.$index, scope.row)">查看</el-button>
          <el-button size="small" @click="handleBalance(scope.$index, scope.row)" v-if="scope.row.status==0 && scope.row.nobalanceMoney>0">结算佣金</el-button>
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          <el-button size="small" @click="handleFrozen(scope.$index, scope.row)">
            {{scope.row.status==0 ? '冻结' : '解冻'}}
          </el-button>
          <el-button size="small" @click="handleQrcode(scope.$index, scope.row)">生成二维码</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <el-pagination
      background
      layout="prev, pager, next"
      :total="totalCount" :page-size="paramObj.limit" @current-change="handleCurrentChange">
    </el-pagination>
    
  </div>

</template>

<script>
import api from "@/api/manager";

const SUCCESS = 0;
const LIMIT = 10;
export default {
  data() {
    return {
      tableData: [],
      totalCount: 0,
      optId: "",
      paramObj: {
        name: null,
        currentPage: 1,
        limit: LIMIT
      }
    };
  },
  created() {
    this._getData();
  },
  methods: {
    statusFormat(row, column) {
      let status = row[column.property];
      if (status == 0) {
        return "正常";
      } else {
        return "冻结";
      }
    },
    handleView(index, row) {
      this.$router.push({ path: "/manager/detail", query: { id: row.id } });
    },
    handleBalance(index, row) {
      this.optId = row.id;

      this.$confirm("确定结算?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this._sureBalance();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消操作"
          });
        });
    },
    handleCurrentChange(val) {
      this.paramObj.currentPage = val;
      this._getData();
    },
    handleDelete(index, row) {
      this.optId = row.id;

      this.$confirm("此操作将永久删除该记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this._sureDelete();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    _sureDelete() {
      api.delete(this.optId).then(response => {
        if (response.code == SUCCESS) {
          this._getData();
          this.$message({
            type: "success",
            message: "删除成功!"
          });
        }
      });
    },
    _sureBalance() {
      api.balanceReward(this.optId).then(response => {
        if (response.code == SUCCESS) {
          this._getData();
          this.$message({
            type: "success",
            message: "操作成功"
          });
        }
      });
    },
    _getData() {
      api.getList(this.paramObj).then(response => {
        if (response.code == SUCCESS) {
          this.tableData = response.data.managerList;
          this.totalCount = response.data.totalCount;
        }
      });
    },
    handleEdit(index, row) {
      this.$router.push({ path: "/manager/edit", query: { id: row.id } });
    },
    create() {
      this.$router.push({ path: "/manager/edit" });
    },
    handleFrozen(index, row) {
      let status;
      if (row.status == 0) {
        status = 1;
      } else {
        status = 0;
      }
      api.setFrozen(row.id, status).then(response => {
        if (response.code == SUCCESS) {
          this._getData();
          this.$message({
            type: "success",
            message: "设置成功!"
          });
        }
      });
    },
    search() {
      this.paramObj.currentPage = 1;
      this._getData();
    },
    handleQrcode(index, row) {
      api.getQRcode(row.id).then(response => {
        if (response.code == SUCCESS) {
          this.downloadQRcode(response.data,row.name);
        }
      });
    },
    downloadQRcode(imgPath,managerName) {
      window.location.href = "/api/export/qrcode?imgPath=" + imgPath+"&managerName="+managerName;
    }
  }
};
</script>

<style lang="less">
.managerWrapper {
  .handle-box {
    margin-bottom: 20px;
    .handle-select {
      width: 240px;
    }
    .handle-input {
      width: 200px;
      display: inline-block;
    }
    .create-btn {
      float: right;
      margin-right: 20px;
    }
  }
}
</style>